package net.pws.oos.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.pws.common.security.filter.CaptchaValidator;

import com.octo.captcha.service.image.ImageCaptchaService;


public class JCaptchaValidator implements CaptchaValidator {
    
    private ImageCaptchaService jcaptchaService;
    
    private String captchaParameter = "j_code";
    
    public boolean validate(HttpServletRequest request,
                            HttpServletResponse response) {
        String sessionId = request.getSession().getId();
        String code = request.getParameter(captchaParameter);
        boolean valid = false;
        
        try {
            valid = jcaptchaService.validateResponseForID(sessionId, code);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        
        return valid;
    }
    
    public void setJcaptchaService(ImageCaptchaService jcaptchaService) {
        this.jcaptchaService = jcaptchaService;
    }
    
    public void setCaptchaParameter(String captchaParameter) {
        this.captchaParameter = captchaParameter;
    }
    
}
